package android.translator.views;


import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.ViewPart;

import android.translator.Translator;


/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class AndroidTranslatorView extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "android.translator.views.AndroidTranslatorView";

	//	private TreeViewer viewer;
	//	private TreeObject invisibleRoot;
	private ScrolledForm form;
	private List<Button> targetSelection;

//	class TreeObject implements IAdaptable {
//
//		private String name;
//		private TreeObject parent;
//		private IResource resouce;
//		private ArrayList<TreeObject> children;
//
//		public TreeObject(String name) {
//			this.name = name;
//			children = new ArrayList<TreeObject>();
//		}
//		public void addChild(TreeObject child) {
//			children.add(child);
//			child.setParent(this);
//		}
//
//		public void removeChild(TreeObject child) {
//			children.remove(child);
//			child.setParent(null);
//		}
//
//		public TreeObject[] getChildren() {
//			return (TreeObject[]) children.toArray(new TreeObject[children.size()]);
//		}
//
//		public boolean hasChildren() {
//			return children.size() > 0;
//		}
//
//		public String getName() {
//			return name;
//		}
//
//		public void setParent(TreeObject parent) {
//			this.parent = parent;
//		}
//
//
//		public TreeObject getParent() {
//			return parent;
//		}
//
//		public String toString() {
//			return getName();
//		}
//
//
//		public Object getAdapter(@SuppressWarnings("rawtypes") Class key) {
//			return null;
//		}
//
//		protected IResource getResouce() {
//			return resouce;
//		}
//
//		protected void setResouce(IResource resouce) {
//			this.resouce = resouce;
//		}
//	}

	//	class ViewContentProvider implements ITreeContentProvider {
	//
	//		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
	//		}
	//		public void dispose() {
	//		}
	//
	//		public Object[] getElements(Object parent) {
	//			if (parent.equals(getViewSite())) {
	//				if (invisibleRoot == null)
	//					initialize();
	//
	//				return getChildren(invisibleRoot);
	//			}
	//
	//			return getChildren(parent);
	//		}
	//
	//		public Object getParent(Object child) {
	//			if (child instanceof TreeObject) {
	//				return ((TreeObject) child).getParent();
	//			}
	//			return null;
	//		}
	//
	//		public Object[] getChildren(Object parent) {
	//			if (parent instanceof TreeObject) {
	//				return ((TreeObject) parent).getChildren();
	//			}
	//
	//			return new Object[0];
	//		}
	//
	//		public boolean hasChildren(Object parent) {
	//			if (parent instanceof TreeObject)
	//				return ((TreeObject) parent).hasChildren();
	//			return false;
	//		}
	//
	//	}
	//	class ViewLabelProvider extends LabelProvider {
	//
	//		public String getText(Object obj) {
	//			return obj.toString();
	//		}
	//
	//		public Image getImage(Object obj) {
	//			String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
	//
	//			if (obj instanceof TreeObject) {
	//				TreeObject node = (TreeObject)obj;
	//				if (node.getName().equals("Android Resource Files")) {
	//					imageKey = ISharedImages.IMG_OBJ_PROJECT;
	//				}
	//				else if (node.hasChildren()) {
	//					imageKey = ISharedImages.IMG_OBJ_FOLDER;
	//				}
	//			}
	//			return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
	//		}
	//	}

		public void setLanguageSelection() {
			try {
				IWorkspace workspace = ResourcesPlugin.getWorkspace();
	
				IProject[] projects = workspace.getRoot().getProjects();
	
				for (int i = 0; i < projects.length; i++) {
					IResource[] folderResources = projects[i].members();
					for (int j = 0; j < folderResources.length; j++) {
	
						if (folderResources[j] instanceof IFolder) {
							IFolder resource = (IFolder) folderResources[j];
							if (resource.getName().equalsIgnoreCase("res")) {
								// look in res folder
								IResource[] valuesResources = resource.members();
								for (int k = 0; k < valuesResources.length; k++) {
									if (valuesResources[k] instanceof IFolder && valuesResources[k].getName().startsWith("values")){
										IResource[] stringResources = ((IFolder)valuesResources[k]).members();
										for (int l = 0; l < stringResources.length;l++) {
											if (stringResources[l].getName().equals("strings.xml")) {
//												System.out.println(valuesResources[k].getName()+">> "+stringResources[l]);
												Translator.setLanguageSelection(valuesResources[k].getName(),targetSelection);
											}
										}
									}
									else {
//										System.out.println("3Ignoring "+valuesResources[k].getName());
									}
								}
							}
							else {
//								System.out.println("2Ignoring "+resource.getName());
							}
						}
						else {
//							System.out.println("1Ignoring "+folderResources[j].getName());
						}
					}
				}
			}
			catch (Exception e) {
				// log exception
				e.printStackTrace();
			}
		}

	//	private void createToolbar() {
	//		Action translate =new Action() {
	//			public void run() {
	//				// todo
	//			}
	//		};
	//		IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
	//		mgr.add(translate);
	//	}


	public void createPartControl(Composite parent) {
		//		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		//		viewer.setContentProvider(new ViewContentProvider());
		//		viewer.setLabelProvider(new ViewLabelProvider());
		//		viewer.setInput(getViewSite());
		//		createToolbar();
		//		hookContextMenu();
		//		hookDoubleClickAction();
		FormToolkit toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);
		GridLayout layout = new GridLayout();
		layout.numColumns = 6;
		form.getBody().setLayout(layout);

		toolkit.createLabel(form.getBody(), "Translate from :");
		//		Text text = toolkit.createText(form.getBody(), "");
		String[] languages = Translator.getLanguages();
		Combo combo = new Combo(form.getBody(), SWT.DROP_DOWN | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		combo.setItems(languages);
		combo.select(Translator.getDefault());
		//		combo.setLayoutData(new GridData(GridData.BEGINNING));
		Button goButton = toolkit.createButton(form.getBody(), "Go", SWT.BUTTON1);
		toolkit.createLabel(form.getBody(), "");
		toolkit.createLabel(form.getBody(), "");
		toolkit.createLabel(form.getBody(), "");

		targetSelection = new ArrayList<Button>();
		toolkit.createLabel(form.getBody(), "Translate to :");
		for (int i = 0; i < languages.length;i++) {
			Button button = toolkit.createButton(form.getBody(), languages[i], SWT.CHECK);
			targetSelection.add(button);
		}
		setLanguageSelection();
	}


	//	private void hookDoubleClickAction() {
	//		viewer.addDoubleClickListener(new IDoubleClickListener() {
	//			public void doubleClick(DoubleClickEvent event) {
	//				ISelection selection = event.getSelection();
	//				Object obj = ((IStructuredSelection) selection).getFirstElement();
	//				if (!(obj instanceof TreeObject)) {
	//					return;
	//				}
	//				else {
	//					TreeObject tempObj = (TreeObject) obj;
	//					IFile ifile = ResourcesPlugin.getWorkspace().getRoot().
	//					getFile(tempObj.getResouce().getFullPath());
	//					IWorkbenchPage dpage = AndroidTranslatorView.this.getViewSite().getWorkbenchWindow().getActivePage();
	//					if (dpage != null) {
	//						try {
	//							IDE.openEditor(dpage, ifile,true);
	//						}
	//						catch (Exception e) {
	//							// log exception
	//							e.printStackTrace();
	//						}
	//					}
	//				}
	//			};
	//		});
	//	}

	//	private void hookContextMenu() {
	//		MenuManager menuMgr = new MenuManager("#PopupMenu");
	//		Menu menu = menuMgr.createContextMenu(viewer.getControl());
	//		viewer.getControl().setMenu(menu);
	//		Action translate =new Action() {
	//			public void run() {
	//				// todo
	//			}
	//		};
	//		translate.setText("Translate from this source");
	//		menuMgr.add(translate);
	//
	//		//		Action refresh =new Action() {
	//		//			public void run() {
	//		//				initialize();
	//		//				viewer.refresh();
	//		//			}
	//		//		};
	//		//		refresh.setText("Refresh");
	//		//		menuMgr.add(refresh);
	//	}

	public void setFocus() {
		form.setFocus();
		System.out.println("<><><><><>setFocus<><><><>");
	}
}
